//
//  Animation.swift
//  DemoUI
//
//  Created by 白瑾浩 on 2022/1/6.
//

import Foundation
import UIKit

class Animation : UIViewController,UITextFieldDelegate {
    var textField:UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        
        //Array<UIImage>()
        var imageArray = [UIImage]()
        for index in 1...7 {
            let birdImageView = UIImage(named: "bird\(index)")
            imageArray.append( birdImageView! )
        }
        let mUIImageView = UIImageView(frame: CGRect(x: 10, y: 100, width: 30, height: 30))
        
        mUIImageView.animationImages = imageArray
        mUIImageView.animationDuration = 4
        mUIImageView.animationRepeatCount = 0
        
        self.view.addSubview( mUIImageView  )
        //开始动画
        mUIImageView.startAnimating()
        
        //表单
        
        textField = UITextField(frame:  CGRect(x: 40, y: 150, width: 200, height: 30))
        //凡赛尔风格边框
        //多种风格
        //UITextField.BorderStyle.bezel
        //UITextField.BorderStyle.line
        textField.borderStyle = UITextField.BorderStyle.roundedRect //圆角
        
        textField.textColor = UIColor.red //输入字体红色
        textField.textAlignment = NSTextAlignment.center //文字居中
        textField.placeholder = "请输入姓名" //提示文字
        
        textField.clearButtonMode = .always  //是否显示清除按钮  和  rightView 不能共存，互斥显示
        
        textField.layer.masksToBounds = true //圆角
        textField.layer.cornerRadius = 5
        
        //为UITextField设置左右试图
        let imageView1 = UIImageView(image: UIImage(named: "bird4")!.scaled(to: CGSize(width: 20, height: 20)))
        imageView1.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
        imageView1.contentMode = UIView.ContentMode.center
        
        let imageView2 = UIImageView(image: UIImage(named: "bird5")!.scaled(to: CGSize(width: 20, height: 20)))
        imageView2.frame = CGRect(x: -0, y: 0, width: 20, height: 20)
        imageView2.contentMode = UIView.ContentMode.center
        
        textField.leftView = imageView2
        
        textField.rightView = imageView1
        //never 从不显示
        //whileEditing 只有当控件处于编辑时候才显示
        //unlessEditing 控件不处于编辑状态才显示
        //aways 任何情况都显示
        textField.leftViewMode = UITextField.ViewMode.always //左试图的显示模式 任何情况下都显示
        textField.rightViewMode = UITextField.ViewMode.always //右试图的显示模式 任何情况下都显示
        
        textField.isSecureTextEntry = true //true密码输入框 / false文本
        
        textField.keyboardType = .namePhonePad //输入文本类型
        
        textField.font = UIFont.systemFont(ofSize:  20 ) //字体大小
        
        textField.returnKeyType = UIReturnKeyType.done//键盘完成
        
        //UITextField 控件代理方法
        textField.delegate = self //代理文本监听
        
        self.view.addSubview( textField )
        
        //边距
        
        
        let textField2 = CustomUITextField(headWidth: 20 , frame:  CGRect(x: 40, y: 300, width: 200, height: 30))
        //凡赛尔风格边框
        //多种风格
        //UITextField.BorderStyle.bezel
        //UITextField.BorderStyle.line
        textField2.borderStyle = UITextField.BorderStyle.roundedRect //圆角
        
        textField2.textColor = UIColor.red //输入字体红色
        textField2.textAlignment = NSTextAlignment.left //文字居中
        textField2.placeholder = "请输入姓名" //提示文字
        
        textField2.clearButtonMode = .always  //是否显示清除按钮  和  rightView 不能共存，互斥显示
        
        textField2.layer.masksToBounds = true //圆角
        textField2.layer.cornerRadius = 5
        
        //为UITextField设置左右试图
        let imageView3 = UIImageView(image: UIImage(named: "bird4")!.scaled(to: CGSize(width: 20, height: 20)))
        imageView3.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
        imageView3.contentMode = UIView.ContentMode.left
        
        
        textField2.leftView = imageView3
        //never 从不显示
        //whileEditing 只有当控件处于编辑时候才显示
        //unlessEditing 控件不处于编辑状态才显示
        //aways 任何情况都显示
        textField2.leftViewMode = UITextField.ViewMode.always //左试图的显示模式 任何情况下都显示
        
        textField2.keyboardType = .namePhonePad //输入文本类型
        
        textField2.font = UIFont.systemFont(ofSize:  20 ) //字体大小
        
        textField2.returnKeyType = UIReturnKeyType.done//键盘完成
        
        self.view.addSubview( textField2 )
        
        
    }
    
    //edittext 状态监听
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        print("输入框即将进入编辑状态的时候被调用")
        return true
    }
    func textFieldDidBeginEditing(_ textField: UITextField) {
        print( "输入框已经开始编辑的时候被调用" )
    }
    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
        print( "输入框即将结束编辑时调用" )
        return true
    }
    func textFieldDidEndEditing(_ textField: UITextField) {
        print( "已经结束编辑时被调用" )
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        print( "输入框文本发生变化时被调用" )
        
        if string == "\n"{ // 输入换行符时收起键盘
            textField.resignFirstResponder()
            return false
        }
        
        if  (textField.text?.count)! >= 11 {
            return false
        }
        //只能输入数字
        if( !string.isEmpty ){
            if (string.first)! >= "0" && (string.first)! <= "9" {
                return true
            }else{
                return false
            }
        }
        return true
    }
    
    func textFieldShouldClear(_ textField: UITextField) -> Bool {
        print( "当用户点击输入框中的清除按钮被调用" )
        return true
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        print( "当点击键盘上的return时被调用" )
        
        textField.resignFirstResponder() //收起键盘
        return true
    }
    //点击空白处收起软键盘
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?){
        textField.resignFirstResponder()
    }
}

extension UIImage{
    
    func scaled(to newSize: CGSize) -> UIImage {
        //计算比例
        let aspectWidth  = newSize.width/size.width
        let aspectHeight = newSize.height/size.height
        let aspectRatio = max(aspectWidth, aspectHeight)
        
        //图片绘制区域
        var scaledImageRect = CGRect.zero
        scaledImageRect.size.width  = size.width * aspectRatio
        scaledImageRect.size.height = size.height * aspectRatio
        scaledImageRect.origin.x    = (newSize.width - size.width * aspectRatio) / 2.0
        scaledImageRect.origin.y    = (newSize.height - size.height * aspectRatio) / 2.0
        
        //绘制并获取最终图片
        UIGraphicsBeginImageContext(newSize)
        draw(in: scaledImageRect)
        let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return scaledImage!
    }
}

//左右 图片的边距
class CustomUITextField:UITextField{
    var headWidth = 0
    convenience init( headWidth : Int , frame:  CGRect) {
        self.init(frame:frame)
        self.headWidth = headWidth
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func rightViewRect(forBounds bounds: CGRect) -> CGRect {
        var textRect = bounds
        textRect.origin.x = bounds.origin.x - 10
        textRect.size.width = CGFloat(headWidth)
        return textRect;
    }
    override func leftViewRect(forBounds bounds: CGRect) -> CGRect {
        var textRect = bounds
        textRect.origin.x = bounds.origin.x + 10
        textRect.size.width = CGFloat(headWidth)
        return textRect
    }
}
